package com.gcb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.gcb.common.enums.WharfStatus;
import com.gcb.domain.bo.DockBIBO;
import com.gcb.domain.dto.WharfQueryDTO;
import com.gcb.domain.dto.WharfSaveDTO;
import com.gcb.domain.entity.Wharf;
import com.gcb.domain.vo.WharfVO;
import com.gcb.mapper.WharfMapper;
import com.gcb.service.IWharfService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WharfServiceImpl implements IWharfService {

    @Resource
    private WharfMapper wharfMapper;

    @Override
    public void add(WharfSaveDTO dto, Long currentUserId) {
        Wharf wharf = new Wharf();
        BeanUtil.copyProperties(dto, wharf);
        wharf.setFullAddress(dto.getProvince() + dto.getCity() + dto.getDistrict() + dto.getAddress());
        wharf.setStatus(WharfStatus.OK.getCode());
        wharf.setCreateBy(currentUserId);
        wharf.setUpdateBy(currentUserId);
        wharfMapper.add(wharf);
    }

    @Override
    public void update(WharfSaveDTO dto, Long currentUserId) {
        Wharf wharf = new Wharf();
        BeanUtil.copyProperties(dto, wharf);
        wharf.setFullAddress(dto.getProvince() + dto.getCity() + dto.getDistrict() + dto.getAddress());
        wharf.setUpdateBy(currentUserId);
        wharfMapper.update(wharf);
    }

    @Override
    public List<WharfVO> list(WharfQueryDTO dto) {
        List<WharfVO> list = wharfMapper.list(dto);
        list.forEach(this::handleVO);
        return list;
    }

    @Override
    public WharfVO getDetail(Long id) {
        WharfVO byId = wharfMapper.getById(id);
        handleVO(byId);
        return byId;
    }

    private void handleVO(WharfVO vo){
        vo.setStatusStr(WharfStatus.getInfo(vo.getStatus()));
    }

    @Override
    public void batchUpdateStatus(List<Long> ids, Integer status, Long currentUserId) {
        wharfMapper.batchUpdateStatus(ids, status, currentUserId);
    }

    @Override
    public void batchUpdateBusinesses(List<Long> ids, Long businessesId, Long currentUserId) {
        wharfMapper.batchUpdateBusinesses(ids, businessesId, currentUserId);
    }

    @Override
    public List<Wharf> getByBusinessesId(Long businessesId) {
        return wharfMapper.getByBusinessesId(businessesId);
    }

    @Override
    public void delete(Long id) {
        wharfMapper.delete(id);
    }

    @Override
    public List<DockBIBO> biList(Integer flag , String city) {
        return wharfMapper.selectBIList(flag , city);
    }
}
